package com.supor.aiot.mine.utils;

import android.graphics.Rect;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;
import android.widget.ScrollView;

import com.android.baseconfig.common.utils.Logc;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import androidx.core.widget.NestedScrollView;
import kotlin.jvm.internal.Intrinsics;

/**
 * ---------------------------------------------------------------- <br>
 * Copyright (C) 2014-2021, by het, ShenZhen, All rights reserved.  <br>
 * ---------------------------------------------------------------- <br>
 * <p>
 * 描述: InputHandleUtil <br>
 * 作者: lei <br>
 * 日期: 2021/9/7 <br>
 */
public class InputHandleUtil {
    private ViewGroup mRootView;
    private FrameLayout.LayoutParams rootViewParams;
    private int rootViewMargin;
    private int lastRootLayoutHeight;
    private ViewGroup.MarginLayoutParams scrollViewParams;
    private int scrollViewMargin;
    private ViewGroup.MarginLayoutParams ignoreViewParams;

    /**
     *
     * （1）rootView：必传参数，系统的根布局view （android.R.id.content）
     * （2）scrollView：非必传，包含 EditText 的 ScrollView
     * （3）ignoreView：非必传，想要忽略的View，键盘弹出时，不显示此 view，例如：底部提交按钮
     *
     * 作者：左郁丶
     * 链接：https://www.jianshu.com/p/325c1cf72038
     * @param rootView
     * @param scrollView
     * @param ignoreView
     */
    public final void handleInputView(@NotNull ViewGroup rootView,
                                      @Nullable final NestedScrollView scrollView,
                                      @Nullable final View ignoreView) {
        Intrinsics.checkParameterIsNotNull(rootView, "rootView");
        View root = rootView.getChildAt(0);
        if (root == null) {
            Logc.e("null cannot be cast to non-null type android.view.ViewGroup");
        } else {
            this.mRootView = (ViewGroup) root;
            android.view.ViewGroup.LayoutParams params = mRootView.getLayoutParams();
            if (params == null) {
                Logc.e("null cannot be cast to non-null type android.widget" +
                        ".FrameLayout.LayoutParams");
            } else {
                this.rootViewParams = (FrameLayout.LayoutParams) params;
                this.rootViewMargin = rootViewParams.bottomMargin;
                if (mRootView != null) {
                    ViewTreeObserver viewTreeObserver = mRootView.getViewTreeObserver();
                    if (viewTreeObserver != null) {
                        viewTreeObserver.addOnGlobalLayoutListener(() ->
                                possiblyResizeChildOfContent(scrollView,
                                ignoreView));
                    }
                }

                this.lastRootLayoutHeight = this.getRootLayoutHeight();
                if (scrollView != null) {
                    params = scrollView.getLayoutParams();
                    if (params == null) {
                        Logc.e("null cannot be cast to non-null type android" +
                                ".view.ViewGroup.MarginLayoutParams");
                        return;
                    }

                    this.scrollViewParams = (ViewGroup.MarginLayoutParams) params;
                    this.scrollViewMargin = scrollViewParams.bottomMargin;
                }

                if (ignoreView != null) {
                    params = ignoreView.getLayoutParams();
                    if (params == null) {
                        Logc.e("null cannot be cast to non-null type android" +
                                ".view.ViewGroup.MarginLayoutParams");
                        return;
                    }

                    this.ignoreViewParams = (ViewGroup.MarginLayoutParams) params;
                }

            }
        }
    }

    public final void handleInputView(@NotNull ViewGroup rootView,
                                      @Nullable final ScrollView scrollView,
                                      @Nullable final View ignoreView) {
        Intrinsics.checkParameterIsNotNull(rootView, "rootView");
        View root = rootView.getChildAt(0);
        if (root == null) {
            Logc.e("null cannot be cast to non-null type android.view" +
                    ".ViewGroup");
        } else {
            this.mRootView = (ViewGroup) root;
            android.view.ViewGroup.LayoutParams params = mRootView.getLayoutParams();
            if (params == null) {
                Logc.e("null cannot be cast to non-null type android.widget" +
                        ".FrameLayout.LayoutParams");
            } else {
                this.rootViewParams = (FrameLayout.LayoutParams) params;
                this.rootViewMargin = rootViewParams.bottomMargin;
                if (mRootView != null) {
                    ViewTreeObserver viewTreeObserver = mRootView.getViewTreeObserver();
                    if (viewTreeObserver != null) {
                        viewTreeObserver.addOnGlobalLayoutListener(() ->
                                possiblyResizeChildOfContent(scrollView,
                                ignoreView));
                    }
                }

                this.lastRootLayoutHeight = this.getRootLayoutHeight();
                if (scrollView != null) {
                    params = scrollView.getLayoutParams();
                    if (params == null) {
                        Logc.e("null cannot be cast to non-null type android" +
                                ".view.ViewGroup.MarginLayoutParams");
                        return;
                    }

                    this.scrollViewParams = (ViewGroup.MarginLayoutParams) params;
                    this.scrollViewMargin = scrollViewParams.bottomMargin;
                }

                if (ignoreView != null) {
                    params = ignoreView.getLayoutParams();
                    if (params == null) {
                        Logc.e("null cannot be cast to non-null type android" +
                                ".view.ViewGroup.MarginLayoutParams");
                        return;
                    }

                    this.ignoreViewParams = (ViewGroup.MarginLayoutParams) params;
                }

            }
        }
    }

    private void possiblyResizeChildOfContent(ScrollView scrollView, View ignoreView) {
        int currentRootLayoutHeight = this.getRootLayoutHeight();
        if (currentRootLayoutHeight != this.lastRootLayoutHeight) {
            int rootHeight = 0;
            ViewGroup viewGroup = this.mRootView;
            if (viewGroup != null) {
                View view = viewGroup.getRootView();
                if (view != null) {
                    rootHeight = view.getHeight();
                }
            }

            int rootLayoutHeight = rootHeight;
            int keyBoardHeight = rootLayoutHeight - currentRootLayoutHeight;
            ViewGroup.MarginLayoutParams marginLayoutParams;
            FrameLayout.LayoutParams layoutParams;
            if (keyBoardHeight > rootLayoutHeight / 4) {
                Logc.d("possiblyResizeChildOfContent: 软键盘弹出" );
                if (scrollView != null) {
                    int height = ignoreView != null ? ignoreView.getHeight() : 0;
                    marginLayoutParams = this.ignoreViewParams;
                    int topMargin = marginLayoutParams != null ? marginLayoutParams.topMargin : 0;
                    marginLayoutParams = this.ignoreViewParams;
                    int bottomMargin = marginLayoutParams != null ? marginLayoutParams.bottomMargin : 0;
                    marginLayoutParams = this.scrollViewParams;
                    if (marginLayoutParams != null) {
                        marginLayoutParams.bottomMargin = keyBoardHeight - height - topMargin - bottomMargin;
                    }

                    scrollView.requestLayout();
                } else {
                    layoutParams = this.rootViewParams;
                    if (layoutParams != null) {
                        layoutParams.bottomMargin = this.rootViewMargin + keyBoardHeight;
                    }

                    viewGroup = this.mRootView;
                    if (viewGroup != null) {
                        viewGroup.requestLayout();
                    }
                }
            } else if (scrollView != null) {
                Logc.d("possiblyResizeChildOfContent: 软键盘关闭1" );
                marginLayoutParams = this.scrollViewParams;
                if (marginLayoutParams != null) {
                    marginLayoutParams.bottomMargin = this.scrollViewMargin;
                }

                scrollView.requestLayout();
                scrollView.clearFocus();
            } else {
                Logc.d("possiblyResizeChildOfContent: 软键盘关闭2" );
                layoutParams = this.rootViewParams;
                if (layoutParams != null) {
                    layoutParams.bottomMargin = this.rootViewMargin;
                }

                viewGroup = this.mRootView;
                if (viewGroup != null) {
                    viewGroup.requestLayout();
                }
            }

            this.lastRootLayoutHeight = currentRootLayoutHeight;
        }

    }
    public final void handleInputView(@NotNull ViewGroup rootView,
                                      @Nullable final View ignoreView) {
        Intrinsics.checkParameterIsNotNull(rootView, "rootView");
        View root = rootView.getChildAt(0);
        if (root == null) {
            Logc.e("null cannot be cast to non-null type android.view" +
                    ".ViewGroup");
        } else {
            this.mRootView = (ViewGroup) root;
            android.view.ViewGroup.LayoutParams params = mRootView.getLayoutParams();
            if (params == null) {
                Logc.e("null cannot be cast to non-null type android.widget" +
                        ".FrameLayout.LayoutParams");
            } else {
                this.rootViewParams = (FrameLayout.LayoutParams) params;
                this.rootViewMargin = rootViewParams.bottomMargin;
                if (mRootView != null) {
                    ViewTreeObserver viewTreeObserver = mRootView.getViewTreeObserver();
                    if (viewTreeObserver != null) {
                        viewTreeObserver.addOnGlobalLayoutListener(() ->
                                possiblyResizeChildOfContent(ignoreView));
                    }
                }

                this.lastRootLayoutHeight = this.getRootLayoutHeight();

                if (ignoreView != null) {
                    params = ignoreView.getLayoutParams();
                    if (params == null) {
                        Logc.e("null cannot be cast to non-null type android" +
                                ".view.ViewGroup.MarginLayoutParams");
                        return;
                    }

                    this.ignoreViewParams = (ViewGroup.MarginLayoutParams) params;
                }

            }
        }
    }

    private void possiblyResizeChildOfContent(View ignoreView) {
        int currentRootLayoutHeight = this.getRootLayoutHeight();
        if (currentRootLayoutHeight != this.lastRootLayoutHeight) {
            int rootHeight = 0;
            ViewGroup viewGroup = this.mRootView;
            if (viewGroup != null) {
                View view = viewGroup.getRootView();
                if (view != null) {
                    rootHeight = view.getHeight();
                }
            }

            int rootLayoutHeight = rootHeight;
            int keyBoardHeight = rootLayoutHeight - currentRootLayoutHeight;
            ViewGroup.MarginLayoutParams marginLayoutParams;
            FrameLayout.LayoutParams layoutParams;
            if (keyBoardHeight > rootLayoutHeight / 4) {
                Logc.d("possiblyResizeChildOfContent: 软键盘弹出" );

                layoutParams = this.rootViewParams;
                if (layoutParams != null) {
                    layoutParams.bottomMargin = this.rootViewMargin + keyBoardHeight;
                }

                viewGroup = this.mRootView;
                if (viewGroup != null) {
                    viewGroup.requestLayout();
                }
            } else {
                Logc.d("possiblyResizeChildOfContent: 软键盘关闭2" );
                layoutParams = this.rootViewParams;
                if (layoutParams != null) {
                    layoutParams.bottomMargin = this.rootViewMargin;
                }

                viewGroup = this.mRootView;
                if (viewGroup != null) {
                    viewGroup.requestLayout();
                }
            }

            this.lastRootLayoutHeight = currentRootLayoutHeight;
        }

    }

    private void possiblyResizeChildOfContent(NestedScrollView scrollView, View ignoreView) {
        int currentRootLayoutHeight = this.getRootLayoutHeight();
        if (currentRootLayoutHeight != this.lastRootLayoutHeight) {
            int rootHeight = 0;
            ViewGroup viewGroup = this.mRootView;
            if (viewGroup != null) {
                View view = viewGroup.getRootView();
                if (view != null) {
                    rootHeight = view.getHeight();
                }
            }

            int rootLayoutHeight = rootHeight;
            int keyBoardHeight = rootLayoutHeight - currentRootLayoutHeight;
            ViewGroup.MarginLayoutParams marginLayoutParams;
            FrameLayout.LayoutParams layoutParams;
            if (keyBoardHeight > rootLayoutHeight / 4) {
                Logc.d("possiblyResizeChildOfContent: 软键盘弹出" );
                if (scrollView != null) {
                    int height = ignoreView != null ? ignoreView.getHeight() : 0;
                    marginLayoutParams = this.ignoreViewParams;
                    int topMargin = marginLayoutParams != null ? marginLayoutParams.topMargin : 0;
                    marginLayoutParams = this.ignoreViewParams;
                    int bottomMargin = marginLayoutParams != null ? marginLayoutParams.bottomMargin : 0;
                    marginLayoutParams = this.scrollViewParams;
                    if (marginLayoutParams != null) {
                        marginLayoutParams.bottomMargin = keyBoardHeight - height - topMargin - bottomMargin;
                    }

                    scrollView.requestLayout();
                } else {
                    layoutParams = this.rootViewParams;
                    if (layoutParams != null) {
                        layoutParams.bottomMargin = this.rootViewMargin + keyBoardHeight;
                    }

                    viewGroup = this.mRootView;
                    if (viewGroup != null) {
                        viewGroup.requestLayout();
                    }
                }
            } else if (scrollView != null) {
                Logc.d("possiblyResizeChildOfContent: 软键盘关闭1" );
                marginLayoutParams = this.scrollViewParams;
                if (marginLayoutParams != null) {
                    marginLayoutParams.bottomMargin = this.scrollViewMargin;
                }

                scrollView.requestLayout();
                scrollView.clearFocus();
            } else {
                Logc.d("possiblyResizeChildOfContent: 软键盘关闭2" );
                layoutParams = this.rootViewParams;
                if (layoutParams != null) {
                    layoutParams.bottomMargin = this.rootViewMargin;
                }

                viewGroup = this.mRootView;
                if (viewGroup != null) {
                    viewGroup.requestLayout();
                }
            }

            this.lastRootLayoutHeight = currentRootLayoutHeight;
        }

    }

    private int getRootLayoutHeight() {
        Rect r = new Rect();
        ViewGroup rootView = this.mRootView;
        if (rootView != null) {
            rootView.getWindowVisibleDisplayFrame(r);
        }

        return r.bottom;
    }
}
